#!/bin/bash
#通过nmap判断指定的端口是否开通,
#1.判断是否存在nmap
#2.判断参数个数
#执行nmap,扫描 grep结果
echoRed(){
    echo -e "\033[31m$1\033[0m"
}
echoGreen(){
    echo -e "\033[32m$1\033[0m"
}
if ! which nmap ;then
    echoRed "未安装nmap"
    exit 1
fi

if [[ $# -ne 2 ]];then
    echoRed "参数错误,"
    echoGreen "usage:$0 ip port"
    exit 1
fi
ip=$1
port=$2
result=$(nmap -n -sS -p$port $ip | grep "^$port/tcp open")
if [[ $? -eq 0 ]];then
    echoGreen "$ip:$port can accessed!!"
else
    echoGreen "$ip:$port can't accessed!!"
fi
